[Java]Queue in while loop, cannot modify the value?

Posted by javaLearner.java on Stack Overflow See other posts from Stack Overflow or by javaLearner.java
Published on 2010-05-11T18:14:57Z Indexed on 2010/05/11 18:34 UTC
Read the original article Hit count: 153

Filed under:
|
|
|

This is my code:

Iterator it = queue.iterator();

while(it.hasNext()){
   random = randNumber(1,2);
        if(random == 1){
            queue.poll();
        } else {
            queue.add("new");
            queue.poll();
        }
}

It gives me:

Exception in thread "test" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
    at java.util.LinkedList$ListItr.next(LinkedList.java:696)

Edit @Jon Skeet:

What I want to do is:

  • I have a queue list in, let say the size is 10, lets say: a,b,c,d ... j
  • Generate a number between 1 and 2. if 1, pull (remove the top element) else if 2 add new element
  • I will stop the loop until I added 3 new elements

© Stack Overflow or respective owner

Related posts about java

Related posts about queue